package com.shenghui.enterprise.base.controller;

import com.shenghui.enterprise.base.entity.BaseDataDictionary;
import com.shenghui.enterprise.vo.Result;
import com.shenghui.enterprise.base.service.IBaseDataDictionaryService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 字典主表 前端控制器
 * @Author houjh
 * @Email: happyxiaohou@gmail.com
 * @Date: 2020-06-01
 * @Copyright (c) 2020-2022 云南昇晖科技有限公司
 */
@RestController
@RequestMapping("/admin/baseDataDictionary" )
@Api(value = "字典主表接口",description = "用作字典主表演示")
public class BaseDataDictionaryController {

    @Autowired
    private IBaseDataDictionaryService baseDataDictionaryService;


    /**
     * 字典列表
     * @param map
     * @return
     */
    @RequestMapping("/getDataDictionaryList")
    public Object getDataDictionaryList(@RequestParam Map map){
        try {
            return baseDataDictionaryService.getDataDictionaryList(map);
        } catch(Exception e) {
            e.printStackTrace();
            return Result.error( "字典查询失败");
        }
    }

    /**
     * 添加字典
     * @param basedataDictionary
     * @return
     */
    @RequestMapping("/addDataDictionary")
    public Object addDataDictionary(BaseDataDictionary basedataDictionary){
        try {
            return baseDataDictionaryService.addDataDictionary(basedataDictionary);
        } catch(Exception e) {
            e.printStackTrace();
            return Result.error( "字典添加失败");
        }
    }

    /**
     * 删除字典
     * @param id
     * @return
     */
    @RequestMapping("/deleteDataDictionary")
    public Object deleteDataDictionary(String id){
        try {
            return baseDataDictionaryService.deleteDataDictionary(id);
        } catch(Exception e) {
            e.printStackTrace();
            return Result.error( "字典删除失败");
        }
    }

    /**
     * 通过ID查询字典信息
     * @param id
     * @return
     */
    @RequestMapping("/getDataDictionaryById")
    public Object getDataDictionaryById(String id){
        try {
            return baseDataDictionaryService.getDataDictionaryById(id);
        } catch(Exception e) {
            e.printStackTrace();
            return Result.error( "字典查询失败");
        }
    }

    /**
     * 通过CODE查询字典
     * @param code
     * @return
     */
    @RequestMapping("/getDataDictionaryDetailListByCode")
    @ResponseBody
    public Object getDataDictionaryDetailListByCode(String code){
        try {
            return baseDataDictionaryService.getDataDictionaryDetailListByCode(code);
        } catch(Exception e) {
            e.printStackTrace();
            return Result.error( "字典查询失败");
        }
    }

    /**
     * 通过CODE和PCODE查询字典
     * @param pCode
     * @param iCode
     * @return
     */
    @RequestMapping("/getDicByDicCode")
    public Result getDicByDicCode(String pCode, String iCode) {
        try {
            List<Map<String, Object>> result = new ArrayList<>();
            if (!StringUtils.isEmpty(iCode)) {
                result = baseDataDictionaryService.getDicDetailByCode(iCode, pCode);
            }
            return Result.ok( "字典查询成功", result);
        } catch(Exception e) {
            e.printStackTrace();
            return Result.error( "字典查询失败");
        }
    }

    /**
     * 通过Code 查询字典信息
     * @param code
     * @return
     */
    @RequestMapping("/getAllDataListByCode")
    public Result getAllDataListByCode(String code) {
        try {
            List<Map> allDataListByCode = baseDataDictionaryService.getAllDataListByCode(code);
            return Result.ok("查询成功",allDataListByCode);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error( "查询失败");
        }
    }

    /**
     * 根据父级code查询子集以及子集详细
     * @param code
     * @return
     */
    @RequestMapping("/getDataDictionaryTreeListByCode")
    public Result getDataDictionaryTreeListByCode(String code){
        try {
            Result result = baseDataDictionaryService.getDataDictionaryTreeListByCode(code);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error( "查询失败");
        }
    }
}
